package com.jz.netthings.system.controller;

import com.jz.netthings.annotation.AdscmLogger;
import com.jz.netthings.bean.system.JyMenu;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.support.SimpleResponse;
import com.jz.netthings.system.service.JyMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yyy
 * @ClassName AdscmMenuController
 * @Description 菜单管理Controller
 * @date 2018/8/6 10:10
 */
@RestController
@RequestMapping("/adscm/system/menu")
@CrossOrigin
public class AdscmMenuController {

    @Autowired
    JyMenuService jyMenuService;

    /**
     * 增加菜单
     * @param menu
     * @return
     */
    @AdscmLogger("增加菜单")
    @RequestMapping("/addMenu")
    public SimpleResponse addMenu(@RequestBody JyMenu menu) throws AdscmSystemException {

        jyMenuService.addMenu(menu);

        return new SimpleResponse("200","增加菜单成功",menu);

    }
    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @AdscmLogger("删除菜单")
    @RequestMapping("/delMenu/{menuId}")
    public SimpleResponse delMenu(@PathVariable int menuId) throws AdscmSystemException{

//        jyMenuService.deleteById(menuId);
        jyMenuService.delMenu(menuId);
        return new SimpleResponse("200","删除菜单成功",null);

    }
    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @AdscmLogger("修改菜单")
    @RequestMapping("/editMenu")
    public SimpleResponse editMenu(@RequestBody JyMenu menu) throws AdscmSystemException{

        jyMenuService.editMenu(menu);

        return new SimpleResponse("200","修改菜单成功",menu);

    }
    /**
     * 根据父级菜单ID获取子集菜单
     * @param parentMenuId
     * @return
     */
    @AdscmLogger("根据父级菜单ID获取子集菜单")
    @RequestMapping("/getMenu/parentId/{parentMenuId}")
    public SimpleResponse getMenuByParentId(@PathVariable int parentMenuId) throws AdscmSystemException{

        List<JyMenu> menuList = jyMenuService.getMenuByParentId(parentMenuId);

        return new SimpleResponse("200","查询菜单成功",menuList);

    }
    /**
     * 根据菜单名称模糊查询这个菜单以及他的子菜单
     * @param menuName
     * @return
     */
    @AdscmLogger("根据菜单名称模糊查询这个菜单以及他的子菜单")
    @RequestMapping("/getMenuByName")
    public SimpleResponse getMenuByName(String menuName) throws AdscmSystemException{

        List<JyMenu> menuList = jyMenuService.getMenuByName(menuName);

        return new SimpleResponse("200","查询菜单成功",menuList);

    }
    /**
     * 获取整个菜单树
     * @return
     */
    @AdscmLogger("获取整个菜单树")
    @RequestMapping("/getAllMenu")
    public SimpleResponse getAllMenu() throws AdscmSystemException{

        JyMenu menu = jyMenuService.getAllMenuList();

        return new SimpleResponse("200","查询菜单成功",menu);

    }

    @AdscmLogger("获取当前用户指定菜单下面能操作的资源集合")
    @RequestMapping("/getMenuAllSrc/{menuId}")
    public SimpleResponse getMenuAllSrc(@PathVariable int menuId,HttpServletRequest request) throws AdscmSystemException{

        List<JyMenu> menus = jyMenuService.getMenuAllSrc(menuId,request.getParameter("tokenId"));

        return new SimpleResponse("200","获取当前用户指定菜单下面能操作的资源集合成功",menus);

    }
    @AdscmLogger("根据角色获取这个角色可以访问的所有资源集合")
    @RequestMapping("/getRolesSrc/{roleId}")
    public SimpleResponse getRolesSrc(@PathVariable int roleId) throws AdscmSystemException{

        List<JyMenu> menus = jyMenuService.getRolesSrc(roleId);

        return new SimpleResponse("200","查询成功",menus);

    }

    @RequestMapping("/ttt/{menuId}")
    public SimpleResponse getMenuAllSrc(@PathVariable int menuId) throws AdscmSystemException{

        jyMenuService.ttt(menuId);

        return new SimpleResponse("200","ttt",null);

    }

}
